package com.example.meituan.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;

import com.example.meituan.activity.bean.ImgBean;
import com.example.meituan.db.DBUntil;

public class ImgDao {

    private static SQLiteDatabase db=DBUntil.con;

    // 添加图片信息
    public static int addImage(ImgBean image) {
        ContentValues values = new ContentValues();
        values.put("s_id", image.getId());
        values.put("s_shop_id", image.getShopId());
        values.put("s_img", image.getImg());
        try {
            db.insert("d_img", null, values);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    // 删除图片信息
    public static int deleteImageById(String id) {
        return db.delete("d_img", "s_id=?", new String[]{id});
    }

    // 更新图片信息
    public int updateImage(ImgBean image) {
        ContentValues values = new ContentValues();
        values.put("s_shop_id", image.getShopId());
        values.put("s_img", image.getImg());

        return db.update("d_img", values, "s_id=?", new String[]{image.getId()});
    }

    // 查询所有图片信息
    public List<ImgBean> getAllImages() {
        List<ImgBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from d_img", null);

        try {
            while (cursor.moveToNext()) {
                ImgBean img = createImageFromCursor(cursor);
                list.add(img);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return list;
    }

    // 根据 ID 查询单个图片信息
    public static ImgBean getImageById(String id) {
        Cursor cursor = db.rawQuery("select * from d_img where s_id=?", new String[]{id});

        try {
            if (cursor.moveToNext()) {
                return createImageFromCursor(cursor);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return null;
    }

    // 根据商店 ID 查询所有图片信息
    public static List<ImgBean> getAllImagesByShopId(String shopId) {
        List<ImgBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from d_img where s_shop_id=?", new String[]{shopId});

        try {
            while (cursor.moveToNext()) {
                ImgBean img = createImageFromCursor(cursor);
                list.add(img);
            }
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return list;
    }

    // 辅助方法：从游标创建图片对象
    private static ImgBean createImageFromCursor(Cursor cursor) {
        ImgBean img = new ImgBean(
                cursor.getString(cursor.getColumnIndexOrThrow("s_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_shop_id")),
                cursor.getString(cursor.getColumnIndexOrThrow("s_img"))
        );
        return img;
    }
}